package atom.ui.components.stack 
{
	import atom.ui.ComponentContainer;
	import atom.ui.View;
	import com.greensock.easing.Strong;
	import com.greensock.TweenLite;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author Jorge Braccini
	 */
	public class HorizontalStacker extends ComponentContainer {
		
		private var _p:Point;
		private var _highest:int = 0;
		
		public function HorizontalStacker(init:Object=null) {
			super(init);
			_p = new Point();
		}
		
		override protected function initValues():void {
			super.initValues();
			
			init.center = false;
		}
		
		override public function addView(view:View):void {
			super.addView(view);
			calculate(view);
		}
		
		override protected function draw():void {
			super.draw();
			if (viewChildrenNum > 0 && spriteWidth > width) {
				_p = new Point();
				var l:int = viewChildrenNum;
				for (var i:int = 0; i < l; i++) {
					calculate(viewChildren[i]);
				}
			}
			
			if(center) {
				content.x = Math.round(width / 2);
				content.y = Math.round(height / 2);
			}
		}
		
		/////////////////////////////////////////
		// Properties ///////////////////////////
		/////////////////////////////////////////
		
		public function get center():Boolean { return init.center; }
		
		public function set center(value:Boolean):void {
			init.center = value;
			invalidate();
		}
		
		/////////////////////////////////////////
		// Private Methods //////////////////////
		/////////////////////////////////////////
		
		private function calculate(view:View):void {
			if (_p.x + view.width > width) {
				_p.x = 0;
				_p.y += _highest;
				_highest = 0;
			}
			
			view.x = _p.x;
			view.y = _p.y;
			
			_p.x += view.width;
			_highest = view.height > _highest ? view.height : _highest;
		}
		
	}

}